<?php
class Admin_Model_DbTable_Admin extends Zend_Db_Table_Abstract {
	protected $_name = 'admin';
	public function addAdmin($role_id, $img, $admin_name, $password, $email, $phone_number, $address, $status) {
		$data = array (
				'ROLE_ID' => $role_id,
				'IMG' => $img,
				'ADMINNAME' => $admin_name,
				'PASSWORD' => $password,
				'EMAIL' => $email,
				'PHONE_NUMBER' => $phone_number,
				'ADDRESS' => $address,
				'STATUS' => $status 
		);
		return $this->insert ( $data );
	}
	public function updateAdmin($id, $role_id, $img, $admin_name, $password, $email, $phone_number, $address, $status) {
		$data = array (
				'ROLE_ID' => $role_id,
				'IMG' => $img,
				'ADMINNAME' => $admin_name,
				'PASSWORD' => $password,
				'EMAIL' => $email,
				'PHONE_NUMBER' => $phone_number,
				'ADDRESS' => $address,
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function updateStatus($id, $status) {
		$data = array (
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function activeAdmin($id) {
		$lv = $this->fetchRow ( "ID = $id" );
		if ($lv ["STATUS"] == 1) {
			$this->update ( array (
					"STATUS" => 0 
			), "ID = $id" );
		} else {
			$this->update ( array (
					"STATUS" => 1 
			), "ID = $id" );
		}
	}
	public function removeAdmin($id) {
		return $this->delete ( "ID = $id" );
	}
	public function getAdminByAdminNameAndPass($admin_name, $password) {
		$data = $this->fetchAll ( "STATUS = 1" );
		foreach ( $data as $d ) {
			if ($d ['ADMINNAME'] == $admin_name && $d ['PASSWORD'] == $password)
				return $d;
		}
	}
	public function getAllAdmin() {
		$select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)->setIntegrityCheck(false);
		
		$row = null;
		
		$select->from("admin as a")->joinLeft("role as r", "r.ID = a.ROLE_ID",array("r.ROLE_NAME"))
		->group("a.ID");
		
		return $this->fetchAll ($select)->toArray ();
	}
	public function getAdminByRoleName($role_name) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "admin AS ad", array (
				"ad.ID",
				"ad.IMG",
				"ad.ADMINNAME",
				"ad.PASSWORD",
				"ad.EMAIL",
				"ad.PHONE_NUMBER",
				"ad.ADDRESS",
				"ad.STATUS" 
		) )->where ( "ad.STATUS = ?", 1 )->join ( "role AS r", "r.ID = ad.ROLE_ID", array (
				"r.ROLE_NAME" 
		) )->where ( "r.ROLE_NAME= ?", $role_name )->group ( "ad.ID" );
		try {
			$rows = $this->fetchAll ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		
		return $rows->toArray ();
	}
	public function getAdminByRoleId($role_id) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "admin AS ad", array (
				"ad.ID",
				"ad.IMG",
				"ad.ADMINNAME",
				"ad.PASSWORD",
				"ad.EMAIL",
				"ad.PHONE_NUMBER",
				"ad.ADDRESS",
				"ad.STATUS" 
		) )->where ( "ad.STATUS = ?", 1 )->join ( "role AS r", "r.ID = ad.ROLE_ID", array (
				"r.ROLE_NAME" 
		) )->where ( "ad.ROLE_ID = ?", $role_id )->group ( "ad.ID" );
		try {
			$rows = $this->fetchAll ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		
		return $rows->toArray ();
		// return $this->fetchAll ( "ROLE_ID = $role_id", "ADMINNAME DESC"
		// )->toArray ();
		// $name = 'quang';
		// $order = new Zend_Db_Expr($this->getAdapter()->quoteInto("ADMINNAME =
		// ?", $name) ." DESC, `ADMINNAME`");
		// $select = $this->select();
		// $select->order(array("ADMINNAME DESC", "ID ASC"));
		// return $this->fetchAll($select);
	}
	public function getAdminByAdminName($admin_name) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "admin AS ad", array (
				"ad.ID",
				"ad.IMG",
				"ad.ADMINNAME",
				"ad.PASSWORD",
				"ad.EMAIL",
				"ad.PHONE_NUMBER",
				"ad.ADDRESS",
				"ad.STATUS" 
		) )->where ( "ad.STATUS = ?", 1 )->join ( "role AS r", "r.ID = ad.ROLE_ID", array (
				"r.ROLE_NAME" 
		) )->where ( "ad.ADMINNAME = ?", $admin_name );
		try {
			$rows = $this->fetchRow ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		
		return $rows->toArray ();
		// return $this->fetchRow ( "ADMINNAME = '$admin_name'" )->toArray ();
	}
	public function getAdminById($id) {
		return $this->fetchRow ( "ID = $id" )->toArray ();
	}
	public function getAdminByAlphabe() {
		// $order = new Zend_Db_Expr($this->getAdapter()->quoteInto("ADMINNAME
		// =?", $name) ." DESC, ADMINNAME ASC, ID DESC");
		$select = $this->select ();
		$select->order ( array (
				"ADMINNAME ASC",
				"ID DESC" 
		) );
		return $this->fetchAll ( $select );
	}
}

